using System;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp;

namespace CKY.AgentPlatform.Monitoring
{
    /// <summary>
    /// 监控应用服务接口
    /// </summary>
    [RemoteService(IsEnabled = true)]
    public interface IMonitoringAppService : IApplicationService
    {
        /// <summary>
        /// 获取系统状态
        /// </summary>
        /// <returns>系统状态</returns>
        Task<SystemStatus> GetSystemStatusAsync();

        /// <summary>
        /// 获取系统性能指标
        /// </summary>
        /// <returns>性能指标</returns>
        Task<PerformanceMetrics> GetPerformanceMetricsAsync();

        /// <summary>
        /// 获取API统计信息
        /// </summary>
        /// <param name="timeRange">时间范围（小时）</param>
        /// <returns>API统计信息</returns>
        Task<ApiStatistics> GetApiStatisticsAsync(int timeRange = 24);

        /// <summary>
        /// 获取健康检查结果
        /// </summary>
        /// <returns>健康检查结果</returns>
        Task<HealthCheckResult> GetHealthCheckAsync();

        /// <summary>
        /// 获取数据库性能指标
        /// </summary>
        /// <returns>数据库性能指标</returns>
        Task<DatabaseMetrics> GetDatabaseMetricsAsync();

        /// <summary>
        /// 获取缓存性能指标
        /// </summary>
        /// <returns>缓存性能指标</returns>
        Task<CacheMetrics> GetCacheMetricsAsync();

        /// <summary>
        /// 获取内存使用情况
        /// </summary>
        /// <returns>内存使用情况</returns>
        Task<MemoryUsage> GetMemoryUsageAsync();

        /// <summary>
        /// 获取错误日志统计
        /// </summary>
        /// <param name="timeRange">时间范围（小时）</param>
        /// <returns>错误日志统计</returns>
        Task<ErrorStatistics> GetErrorStatisticsAsync(int timeRange = 24);

        /// <summary>
        /// 获取活跃用户统计
        /// </summary>
        /// <param name="timeRange">时间范围（小时）</param>
        /// <returns>活跃用户统计</returns>
        Task<UserActivity> GetUserActivityAsync(int timeRange = 24);

        /// <summary>
        /// 获取资源使用情况
        /// </summary>
        /// <returns>资源使用情况</returns>
        Task<ResourceUsage> GetResourceUsageAsync();

        /// <summary>
        /// 获取系统信息
        /// </summary>
        /// <returns>系统信息</returns>
        Task<SystemInfo> GetSystemInfoAsync();
    }

    /// <summary>
    /// 系统状态
    /// </summary>
    public class SystemStatus
    {
        /// <summary>
        /// 是否健康
        /// </summary>
        public bool IsHealthy { get; set; }

        /// <summary>
        /// 状态描述
        /// </summary>
        public string Status { get; set; }

        /// <summary>
        /// 运行时间
        /// </summary>
        public TimeSpan Uptime { get; set; }

        /// <summary>
        /// 最后检查时间
        /// </summary>
        public DateTime LastCheck { get; set; }

        /// <summary>
        /// 版本信息
        /// </summary>
        public string Version { get; set; }
    }

    /// <summary>
    /// 性能指标
    /// </summary>
    public class PerformanceMetrics
    {
        /// <summary>
        /// CPU使用率
        /// </summary>
        public double CpuUsage { get; set; }

        /// <summary>
        /// 内存使用率
        /// </summary>
        public double MemoryUsage { get; set; }

        /// <summary>
        /// 磁盘使用率
        /// </summary>
        public double DiskUsage { get; set; }

        /// <summary>
        /// 网络IO
        /// </summary>
        public NetworkIo NetworkIo { get; set; }

        /// <summary>
        /// 线程数
        /// </summary>
        public int ThreadCount { get; set; }

        /// <summary>
        /// 请求队列长度
        /// </summary>
        public int RequestQueueLength { get; set; }

        /// <summary>
        /// 活动连接数
        /// </summary>
        public int ActiveConnections { get; set; }

        /// <summary>
        /// 采集时间
        /// </summary>
        public DateTime Timestamp { get; set; }
    }

    /// <summary>
    /// 网络IO
    /// </summary>
    public class NetworkIo
    {
        /// <summary>
        /// 接收字节数
        /// </summary>
        public long BytesReceived { get; set; }

        /// <summary>
        /// 发送字节数
        /// </summary>
        public long BytesSent { get; set; }

        /// <summary>
        /// 接收包数
        /// </summary>
        public long PacketsReceived { get; set; }

        /// <summary>
        /// 发送包数
        /// </summary>
        public long PacketsSent { get; set; }
    }

    /// <summary>
    /// API统计信息
    /// </summary>
    public class ApiStatistics
    {
        /// <summary>
        /// 总请求数
        /// </summary>
        public long TotalRequests { get; set; }

        /// <summary>
        /// 成功请求数
        /// </summary>
        public long SuccessfulRequests { get; set; }

        /// <summary>
        /// 失败请求数
        /// </summary>
        public long FailedRequests { get; set; }

        /// <summary>
        /// 平均响应时间（毫秒）
        /// </summary>
        public double AverageResponseTime { get; set; }

        /// <summary>
        /// P95响应时间（毫秒）
        /// </summary>
        public double P95ResponseTime { get; set; }

        /// <summary>
        /// P99响应时间（毫秒）
        /// </summary>
        public double P99ResponseTime { get; set; }

        /// <summary>
        /// 每秒请求数
        /// </summary>
        public double RequestsPerSecond { get; set; }

        /// <summary>
        /// 错误率
        /// </summary>
        public double ErrorRate { get; set; }

        /// <summary>
        /// 时间范围（小时）
        /// </summary>
        public int TimeRange { get; set; }
    }

    /// <summary>
    /// 健康检查结果
    /// </summary>
    public class HealthCheckResult
    {
        /// <summary>
        /// 总体健康状态
        /// </summary>
        public string OverallStatus { get; set; }

        /// <summary>
        /// 检查项目列表
        /// </summary>
        public HealthCheckItem[] Checks { get; set; }

        /// <summary>
        /// 检查时间
        /// </summary>
        public DateTime CheckTime { get; set; }
    }

    /// <summary>
    /// 健康检查项目
    /// </summary>
    public class HealthCheckItem
    {
        /// <summary>
        /// 检查项名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 检查项状态
        /// </summary>
        public string Status { get; set; }

        /// <summary>
        /// 检查项描述
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// 检查耗时（毫秒）
        /// </summary>
        public double Duration { get; set; }
    }

    /// <summary>
    /// 数据库性能指标
    /// </summary>
    public class DatabaseMetrics
    {
        /// <summary>
        /// 活动连接数
        /// </summary>
        public int ActiveConnections { get; set; }

        /// <summary>
        /// 空闲连接数
        /// </summary>
        public int IdleConnections { get; set; }

        /// <summary>
        /// 总连接数
        /// </summary>
        public int TotalConnections { get; set; }

        /// <summary>
        /// 平均查询时间（毫秒）
        /// </summary>
        public double AverageQueryTime { get; set; }

        /// <summary>
        /// 慢查询数
        /// </summary>
        public long SlowQueries { get; set; }

        /// <summary>
        /// 查询缓存命中率
        /// </summary>
        public double QueryCacheHitRate { get; set; }

        /// <summary>
        /// 数据库大小（字节）
        /// </summary>
        public long DatabaseSize { get; set; }
    }

    /// <summary>
    /// 缓存性能指标
    /// </summary>
    public class CacheMetrics
    {
        /// <summary>
        /// 缓存项总数
        /// </summary>
        public int TotalItems { get; set; }

        /// <summary>
        /// 内存使用量（字节）
        /// </summary>
        public long MemoryUsage { get; set; }

        /// <summary>
        /// 命中率
        /// </summary>
        public double HitRate { get; set; }

        /// <summary>
        /// 命中次数
        /// </summary>
        public long HitCount { get; set; }

        /// <summary>
        /// 未命中次数
        /// </summary>
        public long MissCount { get; set; }

        /// <summary>
        /// 驱逐次数
        /// </summary>
        public long EvictionCount { get; set; }

        /// <summary>
        /// 平均访问时间（毫秒）
        /// </summary>
        public double AverageAccessTime { get; set; }
    }

    /// <summary>
    /// 内存使用情况
    /// </summary>
    public class MemoryUsage
    {
        /// <summary>
        /// 总内存（字节）
        /// </summary>
        public long TotalMemory { get; set; }

        /// <summary>
        /// 已用内存（字节）
        /// </summary>
        public long UsedMemory { get; set; }

        /// <summary>
        /// 可用内存（字节）
        /// </summary>
        public long AvailableMemory { get; set; }

        /// <summary>
        /// 内存使用率
        /// </summary>
        public double MemoryUsagePercentage { get; set; }

        /// <summary>
        /// GC Gen 0回收次数
        /// </summary>
        public long Gen0Collections { get; set; }

        /// <summary>
        /// GC Gen 1回收次数
        /// </summary>
        public long Gen1Collections { get; set; }

        /// <summary>
        /// GC Gen 2回收次数
        /// </summary>
        public long Gen2Collections { get; set; }
    }

    /// <summary>
    /// 错误统计
    /// </summary>
    public class ErrorStatistics
    {
        /// <summary>
        /// 总错误数
        /// </summary>
        public long TotalErrors { get; set; }

        /// <summary>
        /// 致命错误数
        /// </summary>
        public long FatalErrors { get; set; }

        /// <summary>
        /// 错误数
        /// </summary>
        public long Errors { get; set; }

        /// <summary>
        /// 警告数
        /// </summary>
        public long Warnings { get; set; }

        /// <summary>
        /// 最常见错误类型
        /// </summary>
        public ErrorTypeCount[] TopErrorTypes { get; set; }

        /// <summary>
        /// 时间范围（小时）
        /// </summary>
        public int TimeRange { get; set; }
    }

    /// <summary>
    /// 错误类型统计
    /// </summary>
    public class ErrorTypeCount
    {
        /// <summary>
        /// 错误类型
        /// </summary>
        public string ErrorType { get; set; }

        /// <summary>
        /// 错误数量
        /// </summary>
        public long Count { get; set; }

        /// <summary>
        /// 错误占比
        /// </summary>
        public double Percentage { get; set; }
    }

    /// <summary>
    /// 用户活动统计
    /// </summary>
    public class UserActivity
    {
        /// <summary>
        /// 活跃用户数
        /// </summary>
        public int ActiveUsers { get; set; }

        /// <summary>
        /// 新用户数
        /// </summary>
        public int NewUsers { get; set; }

        /// <summary>
        /// 总会话数
        /// </summary>
        public long TotalSessions { get; set; }

        /// <summary>
        /// 平均会话时长（分钟）
        /// </summary>
        public double AverageSessionDuration { get; set; }

        /// <summary>
        /// 页面浏览量
        /// </summary>
        public long PageViews { get; set; }

        /// <summary>
        /// 时间范围（小时）
        /// </summary>
        public int TimeRange { get; set; }
    }

    /// <summary>
    /// 资源使用情况
    /// </summary>
    public class ResourceUsage
    {
        /// <summary>
        /// CPU使用情况
        /// </summary>
        public CpuUsage Cpu { get; set; }

        /// <summary>
        /// 内存使用情况
        /// </summary>
        public MemoryUsageInfo Memory { get; set; }

        /// <summary>
        /// 磁盘使用情况
        /// </summary>
        public DiskUsageInfo Disk { get; set; }

        /// <summary>
        /// 网络使用情况
        /// </summary>
        public NetworkUsage Network { get; set; }

        /// <summary>
        /// 采集时间
        /// </summary>
        public DateTime Timestamp { get; set; }
    }

    /// <summary>
    /// CPU使用情况
    /// </summary>
    public class CpuUsage
    {
        /// <summary>
        /// CPU使用率
        /// </summary>
        public double UsagePercentage { get; set; }

        /// <summary>
        /// CPU核心数
        /// </summary>
        public int CoreCount { get; set; }

        /// <summary>
        /// 当前频率（MHz）
        /// </summary>
        public double CurrentFrequency { get; set; }

        /// <summary>
        /// 温度（摄氏度）
        /// </summary>
        public double Temperature { get; set; }
    }

    /// <summary>
    /// 内存使用情况信息
    /// </summary>
    public class MemoryUsageInfo
    {
        /// <summary>
        /// 总内存（字节）
        /// </summary>
        public long Total { get; set; }

        /// <summary>
        /// 已用内存（字节）
        /// </summary>
        public long Used { get; set; }

        /// <summary>
        /// 可用内存（字节）
        /// </summary>
        public long Available { get; set; }

        /// <summary>
        /// 内存使用率
        /// </summary>
        public double UsagePercentage { get; set; }
    }

    /// <summary>
    /// 磁盘使用情况
    /// </summary>
    public class DiskUsageInfo
    {
        /// <summary>
        /// 总空间（字节）
        /// </summary>
        public long TotalSpace { get; set; }

        /// <summary>
        /// 已用空间（字节）
        /// </summary>
        public long UsedSpace { get; set; }

        /// <summary>
        /// 可用空间（字节）
        /// </summary>
        public long FreeSpace { get; set; }

        /// <summary>
        /// 磁盘使用率
        /// </summary>
        public double UsagePercentage { get; set; }

        /// <summary>
        /// 磁盘IOPS
        /// </summary>
        public double Iops { get; set; }
    }

    /// <summary>
    /// 网络使用情况
    /// </summary>
    public class NetworkUsage
    {
        /// <summary>
        /// 接收字节数
        /// </summary>
        public long BytesReceived { get; set; }

        /// <summary>
        /// 发送字节数
        /// </summary>
        public long BytesSent { get; set; }

        /// <summary>
        /// 接收包数
        /// </summary>
        public long PacketsReceived { get; set; }

        /// <summary>
        /// 发送包数
        /// </summary>
        public long PacketsSent { get; set; }

        /// <summary>
        /// 带宽使用（Mbps）
        /// </summary>
        public double BandwidthUsage { get; set; }
    }

    /// <summary>
    /// 系统信息
    /// </summary>
    public class SystemInfo
    {
        /// <summary>
        /// 操作系统
        /// </summary>
        public string OperatingSystem { get; set; }

        /// <summary>
        /// .NET版本
        /// </summary>
        public string DotNetVersion { get; set; }

        /// <summary>
        /// 应用程序版本
        /// </summary>
        public string ApplicationVersion { get; set; }

        /// <summary>
        /// 主机名
        /// </summary>
        public string HostName { get; set; }

        /// <summary>
        /// 服务器IP
        /// </summary>
        public string ServerIp { get; set; }

        /// <summary>
        /// 运行时间
        /// </summary>
        public TimeSpan Uptime { get; set; }

        /// <summary>
        /// 启动时间
        /// </summary>
        public DateTime StartTime { get; set; }

        /// <summary>
        /// 进程ID
        /// </summary>
        public int ProcessId { get; set; }

        /// <summary>
        /// 工作目录
        /// </summary>
        public string WorkingDirectory { get; set; }
    }
}